//
//  UploadImageViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/5/31.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "UploadImageViewController.h"
#import "ImagePickerManager.h"
#import "MyCourseListViewModel.h"
#import "UploadViewModel.h"
@interface UploadImageViewController ()
@property (nonatomic, weak)IBOutlet UIImageView *bgView;
@property (nonatomic, weak)IBOutlet UIButton *uploadResultBtn;
@property (nonatomic, weak)IBOutlet UITextView *useFeelText;
@property(nonatomic,strong)MyCourseListViewModel *courseViewModel;
@property(nonatomic,strong)UploadViewModel *uploadViewModel;
@property(nonatomic,strong)NSString *imgUploadedUrl;
@end

@implementation UploadImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"上传照片";
    [self.bgView setImage:[[UIImage imageNamed:@"cornerBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"cornerBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"cornerBg"].size.height/2]];
    [self setNavRightBtn:@"确定" color:[UIColor whiteColor]];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    if (!EC_ISNullStr(self.model.useFeel)) {
       self.useFeelText.text = [NSString stringWithFormat:@"%@",self.model.useFeel];
    }
    [self.uploadResultBtn sd_setImageWithURL:[NSURL URLWithString:self.model.usePicture] forState:UIControlStateNormal];
}
-(IBAction)selectUseFeel:(id)sender{
    [self.courseViewModel fetchFeelSelections:^(NSMutableArray * _Nonnull catList) {
        NSMutableArray *strs = [[NSMutableArray alloc]init];
        for (SysDicModel *model in catList) {
            [strs addObject:model.dictName];
        }
        [SMBRadioPickerView showPickerViewWithStrings:strs completion:^(NSString *selectedString) {
            self.useFeelText.text = selectedString;
        }];
    }];
}
-(IBAction)uploadPhotoBtnPressed:(id)sender{
    if (!EC_ISNullStr(self.model.usePicture)) {
        return;
    }
    [[ImagePickerManager sharedImgPicker]showImgActionWithType:ImagePickerTypePhoto enableEditing:NO Completion:^(UIImage *usericon, UIImagePickerController *imgPicker) {
        [self.uploadViewModel uploadImg:usericon cmp:^(NSString * _Nonnull userIconUrl) {
            self.imgUploadedUrl = userIconUrl;
            [self.uploadResultBtn setImage:nil forState:UIControlStateNormal];
            [self.uploadResultBtn setBackgroundImage:usericon forState:UIControlStateNormal];
        }];
        [self.uploadResultBtn setBackgroundImage:usericon forState:UIControlStateNormal];
        [self.uploadResultBtn setImage:nil forState:UIControlStateNormal];
        [self.uploadResultBtn setTitle:@"" forState:UIControlStateNormal];
        [imgPicker dismissViewControllerAnimated:YES completion:nil];
    }];
}
-(void)rightbuttonPressed:(id)sender{
    kWeakSelf;
    [self.courseViewModel addCourse:[NSString stringWithFormat:@"%.0f",self.model.internalBaseClassIdentifier] useFeel:self.useFeelText.text usePicture:self.imgUploadedUrl cmp:^{
        Toast(@"使用记录添加成功！");
        kStrongSelf;
        [strongSelf.rt_navigationController popViewControllerAnimated:YES complete:nil];
    }];
}
-(MyCourseListViewModel*)courseViewModel{
    if (!_courseViewModel) {
        _courseViewModel = [[MyCourseListViewModel alloc]init];
    }
    return _courseViewModel;
}
-(UploadViewModel*)uploadViewModel{
    if (!_uploadViewModel) {
        _uploadViewModel = [[UploadViewModel alloc]init];
    }
    return _uploadViewModel;
}
-(NSString*)imgUploadedUrl{
    if (!_imgUploadedUrl) {
        _imgUploadedUrl = [[NSString alloc]init];
    }
    return _imgUploadedUrl;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
